/**
 * @author helio
 */
var GeneralLedgerView = Ext.extend(BasicPanel,{
	
	initlize : function() {
		
		if(!this.detailsGrid) {
						
			this.detailsStore = new Ext.data.Store({
			    proxy: new Ext.ux.data.PagingMemoryProxy(this.detailsJson),
			    //remoteSort:true,
			    reader: new Ext.data.JsonReader({
					root: 'ledgers',
					remoteSort: true,
			        fields: ['code', 'client','type', {name:'amount',type:'float'}, 'entry_date', 'status', 'comment']
			    })
			});
			
			// create paging bar
			var pagingBar = new Ext.PagingToolbar({
		        pageSize: 25,
		        store: this.detailsStore,
		        displayInfo: true,
								
				plugins: new Ext.ux.ProgressBarPager()
		    });
			
			// create the CheckboxSelectionModel
			var csm = new Ext.grid.CheckboxSelectionModel({
		        listeners: {
		            rowselect : function(sm,index,record) {
		                if (!record.get('status') && sm.getCount() == 1) {
		                	this.detailsGrid.confirmButton.enable();
		                } else {
		                	this.detailsGrid.confirmButton.disable();
		                }
		                
		            }.createDelegate(this),
		            rowdeselect : function(sm,index,record) {
		                if (!sm.getCount()) {
		                	this.detailsGrid.confirmButton.disable();
		                } 
		            }.createDelegate(this)
		        }
		    });

			// create the grid
			this.detailsGrid = new Ext.grid.GridPanel({
				store: this.detailsStore,
				columns: [csm, {
					header: "日期",
					dataIndex: 'entry_date',
					renderer: Ext.util.Format.dateRenderer('Y-m-d'),
					width: 80,
					sortable: true
				}, {
					header: "客户/厂商名称",
					dataIndex: 'client',
					sortable: true
				}, {
					header: "类型",
					dataIndex: 'type',
					width: 60,
					renderer: SALE.Utils.ledgerTypeRenderer.createDelegate(this),
					sortable: true
				}, {
					header: "金额",
					dataIndex: 'amount',
					align: 'right',		
					width: 70,
					sortable: true
				}, {
					header: "状态",
					dataIndex: 'status',
					width: 50,
					renderer: SALE.Utils.ledgerStatusRenderer.createDelegate(this),
					sortable: true
				}, {
					header: "备注",
					dataIndex: 'comment',
					width: 100
				}],
				
				sm: csm,
				
				tbar:[{
		            text:'结账确认',
		            tooltip:'结账确认',
		            iconCls:'accept',
					handler: this.confirmLedgerBillHandler.createDelegate(this),

		            // Place a reference in the GridPanel
		            ref: '../confirmButton',
		            disabled: true
		        }, '->', {
            		xtype:'splitbutton',
            		text: '过滤',
            		iconCls: 'filter',
            		handler: this.filterLedger.createDelegate(this),
           			menu: [{
						text: '全部显示', 
						handler: this.filterLedger.createDelegate(this)
					}, {
						text: '已结帐帐单', 
						value : "1",
						handler: this.filterLedger.createDelegate(this)
					}, {
						text: '未结帐帐单', 
						value : "0",
						handler: this.filterLedger.createDelegate(this)
					}]
        		}, '-', '选择查看日期：', {
					xtype:'datefield',
				    name: 'entry_date',							
					format:'Y-m-d',
					
					listeners: {
						'select': function(field, date){
							var query = Ext.util.Format.date(date,"Y-m-d");
							SALE.Action.searchGeneralLedger(this.initlizeDataHandler.createDelegate(this),{'entry_date_from':query});
						}.createDelegate(this)
					}
				}],
				
				bbar: pagingBar,
				
				viewConfig: {
			        forceFit: true,
					enableRowBody:true,
		            showPreview:true
			    },
		
				loadMask: true,
				stripeRows: true,
				border: false,
				autoExpandColumn: 'client',
				layout: 'fit',
				
				listeners: {
					cellclick : function(grid, rowIndex, columnIndex, e) {
						this.selectedRowIndex = rowIndex;
					}.createDelegate(this)
				}
			});
		}
	},
	
	initlizeDataHandler : function(resultJson) {
		this.detailsJson['ledgers'] = resultJson.ledgers;
		this.detailsStore.load({params:{start:0, limit:25}});
	},
	
	getCorePanel : function() {
		if (!this.detailsGrid) {
			this.initlize();
			SALE.Action.searchGeneralLedger(this.initlizeDataHandler.createDelegate(this));
		}
		return this.detailsGrid;
	},
	
	filterLedger : function(object) {
		SALE.Action.searchGeneralLedger(this.initlizeDataHandler.createDelegate(this), {
			status: object.value
		});
	},
	
	confirmLedgerBillHandler : function() {
		var recordList = this.detailsGrid.getSelectionModel().getSelections();
		if(recordList.length == 0) {
			this.warnMessage('请先选择收款单！！！', '警告信息');
		} else {
			this.confirmMessage('选择"确定"按钮对帐单进行确定！！！', '提示信息', function(btn){
				if (btn == "ok") {
					var confirmArray = [];
					for (var i = 0; i < recordList.length; i++) {
						var record = recordList[i];
						record.set("status", 1);
						
						var updateJson = record.data;
						updateJson.confirm_status='1';
						
						confirmArray.push(updateJson);
					}
					SALE.Action.savePaymentInfo(this.saveSuccessHandler.createDelegate(this), confirmArray, "confirm");
				}
			}.createDelegate(this));
		} 
		
	}
});

GeneralLedgerView.showLedgerPanel = function(){
	var  ledgerView = new GeneralLedgerView();
	
	SALE.App.showOperateTabPanel(ledgerView.getCorePanel(), "总帐薄");
}